/*
Copyright 2022 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testing

import (
	"context"
	"net/http"

	mock "github.com/stretchr/testify/mock"

	"github.com/dapr/dapr/pkg/channel"
	invokev1 "github.com/dapr/dapr/pkg/messaging/v1"
)

// MockDirectMessaging is an autogenerated mock type for the MockDirectMessaging type
type MockDirectMessaging struct {
	mock.Mock
}

// Invoke provides a mock function with given fields: ctx, targetAppID, req
func (_m *MockDirectMessaging) Invoke(ctx context.Context, targetAppID string, req *invokev1.InvokeMethodRequest) (*invokev1.InvokeMethodResponse, error) {
	ret := _m.Called(ctx, targetAppID, req)

	var r0 *invokev1.InvokeMethodResponse
	if rf, ok := ret.Get(0).(func(context.Context, string, *invokev1.InvokeMethodRequest) *invokev1.InvokeMethodResponse); ok {
		r0 = rf(ctx, targetAppID, req)
	} else if ret.Get(0) != nil {
		r0 = ret.Get(0).(*invokev1.InvokeMethodResponse)
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(context.Context, string, *invokev1.InvokeMethodRequest) error); ok {
		r1 = rf(ctx, targetAppID, req)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

func (_m *MockDirectMessaging) SetAppChannel(appChannel channel.AppChannel) {
	// nop
}

func (_m *MockDirectMessaging) Close() error {
	return nil
}

type FailingDirectMessaging struct {
	Failure           Failure
	SuccessStatusCode int
}

func (f *FailingDirectMessaging) Invoke(ctx context.Context, targetAppID string, req *invokev1.InvokeMethodRequest) (*invokev1.InvokeMethodResponse, error) {
	r, err := req.ProtoWithData()
	if err != nil {
		return &invokev1.InvokeMethodResponse{}, err
	}
	err = f.Failure.PerformFailure(string(r.Message.Data.Value))
	if err != nil {
		return &invokev1.InvokeMethodResponse{}, err
	}
	statusCode := f.SuccessStatusCode
	if statusCode == 0 {
		statusCode = http.StatusOK
	}
	resp := invokev1.
		NewInvokeMethodResponse(int32(statusCode), http.StatusText(statusCode), nil).
		WithRawDataBytes(r.Message.Data.Value)
	return resp, nil
}

func (f *FailingDirectMessaging) SetAppChannel(appChannel channel.AppChannel) {
	// nop
}
